package cn.javazd.xml;

import java.io.File;
import java.io.FileOutputStream;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class Dom4jXml implements Xml {

	@Override
	public void createXml() {
		long startTime = System.currentTimeMillis();
		try {
			Document document = DocumentHelper.createDocument();
			Element app = document.addElement("app");
			Element iphone = app.addElement("iphone");
			iphone.addAttribute("type", "IOS 11.1");

			Element version = iphone.addElement("version");
			version.addText("1.0");

			Element name = iphone.addElement("name");
			name.addText("iphone 8");

			Element color = iphone.addElement("color");
			color.addText("磨砂黑");

			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("UTF-8");
			File file = new File("Dom4j-app.xml");
			XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
			writer.setEscapeText(false);
			writer.write(document);
			writer.close();
			System.out.print("create dom4j xml success ==== ");
			System.out.println("speed times: " + (System.currentTimeMillis() - startTime));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
